using System;
using System.Collections;
using UnityEngine;

namespace Com.LuisPedroFonseca.ProCamera2D
{
	public class ProCamera2DTransitionsFX : BasePC2D
	{
		public static string ExtensionName = "TransitionsFX";

		public Action OnTransitionEnterStarted;

		public Action OnTransitionEnterEnded;

		public Action OnTransitionExitStarted;

		public Action OnTransitionExitEnded;

		public Action OnTransitionStarted;

		public Action OnTransitionEnded;

		private static ProCamera2DTransitionsFX _instance;

		public TransitionsFXShaders TransitionShaderEnter;

		public float DurationEnter = 0.5f;

		public float DelayEnter;

		public EaseType EaseTypeEnter = EaseType.EaseOut;

		public Color BackgroundColorEnter = Color.black;

		public TransitionFXSide SideEnter;

		public TransitionFXDirection DirectionEnter;

		[Range(2f, 128f)]
		public int BlindsEnter = 16;

		public TransitionsFXShaders TransitionShaderExit;

		public float DurationExit = 0.5f;

		public float DelayExit;

		public EaseType EaseTypeExit = EaseType.EaseOut;

		public Color BackgroundColorExit = Color.black;

		public TransitionFXSide SideExit;

		public TransitionFXDirection DirectionExit;

		[Range(2f, 128f)]
		public int BlindsExit = 16;

		public bool StartSceneOnEnterState = true;

		private Coroutine _transitionCoroutine;

		private float _step;

		private Material _transitionEnterMaterial;

		private Material _transitionExitMaterial;

		private Material _currentMaterial;

		public static ProCamera2DTransitionsFX Instance
		{
			get
			{
				if (object.Equals(_instance, null))
				{
					_instance = ProCamera2D.Instance.GetComponent<ProCamera2DTransitionsFX>();
					if (object.Equals(_instance, null))
					{
						throw new UnityException("ProCamera2D does not have a TransitionFX extension.");
					}
				}
				return _instance;
			}
		}

		protected override void Awake()
		{
			base.Awake();
			CreateMaterials();
			if (StartSceneOnEnterState)
			{
				_step = 1f;
				_currentMaterial = _transitionEnterMaterial;
				_currentMaterial.SetFloat("_Step", _step);
			}
		}

		private void OnRenderImage(RenderTexture sourceRenderTexture, RenderTexture destinationRenderTexture)
		{
			sourceRenderTexture.wrapMode = TextureWrapMode.Repeat;
			if (_currentMaterial != null)
			{
				Graphics.Blit(sourceRenderTexture, destinationRenderTexture, _currentMaterial);
			}
			else
			{
				Graphics.Blit(sourceRenderTexture, destinationRenderTexture);
			}
		}

		public void TransitionEnter()
		{
			if (_transitionCoroutine != null)
			{
				StopCoroutine(_transitionCoroutine);
			}
			_transitionCoroutine = StartCoroutine(TransitionRoutine(_transitionEnterMaterial, DurationEnter, DelayEnter, 1f, 0f, EaseTypeEnter));
		}

		public void TransitionExit()
		{
			if (_transitionCoroutine != null)
			{
				StopCoroutine(_transitionCoroutine);
			}
			_transitionCoroutine = StartCoroutine(TransitionRoutine(_transitionExitMaterial, DurationExit, DelayExit, 0f, 1f, EaseTypeExit));
		}

		public void CreateMaterials()
		{
			_transitionEnterMaterial = new Material(Shader.Find("ProCamera2D/TransitionsFX/" + TransitionShaderEnter.ToString()));
			_transitionEnterMaterial.SetColor("_BackgroundColor", BackgroundColorEnter);
			if (TransitionShaderEnter == TransitionsFXShaders.Wipe || TransitionShaderEnter == TransitionsFXShaders.Blinds)
			{
				_transitionEnterMaterial.SetInt("_Direction", (int)SideEnter);
			}
			else if (TransitionShaderEnter == TransitionsFXShaders.Shutters)
			{
				_transitionEnterMaterial.SetInt("_Direction", (int)DirectionEnter);
			}
			_transitionEnterMaterial.SetInt("_Blinds", BlindsEnter);
			_transitionExitMaterial = new Material(Shader.Find("ProCamera2D/TransitionsFX/" + TransitionShaderExit.ToString()));
			_transitionExitMaterial.SetColor("_BackgroundColor", BackgroundColorExit);
			if (TransitionShaderExit == TransitionsFXShaders.Wipe || TransitionShaderExit == TransitionsFXShaders.Blinds)
			{
				_transitionExitMaterial.SetInt("_Direction", (int)SideExit);
			}
			else if (TransitionShaderExit == TransitionsFXShaders.Shutters)
			{
				_transitionExitMaterial.SetInt("_Direction", (int)DirectionExit);
			}
			_transitionExitMaterial.SetInt("_Blinds", BlindsExit);
		}

		public void Clear()
		{
			_currentMaterial = null;
		}

		private IEnumerator TransitionRoutine(Material material, float duration, float delay, float startValue, float endValue, EaseType easeType)
		{
			_step = startValue;
			_currentMaterial = material;
			_currentMaterial.SetFloat("_Step", _step);
			if (endValue == 0f)
			{
				if (OnTransitionEnterStarted != null)
				{
					OnTransitionEnterStarted();
				}
			}
			else if (endValue == 1f && OnTransitionExitStarted != null)
			{
				OnTransitionExitStarted();
			}
			if (OnTransitionStarted != null)
			{
				OnTransitionStarted();
			}
			if (delay > 0f)
			{
				yield return new WaitForSeconds(delay);
			}
			float t = 0f;
			while (t <= 1f)
			{
				t += ProCamera2D.DeltaTime / duration;
				_step = Utils.EaseFromTo(startValue, endValue, t, easeType);
				material.SetFloat("_Step", _step);
				yield return null;
			}
			_step = endValue;
			material.SetFloat("_Step", _step);
			if (endValue == 0f)
			{
				if (OnTransitionEnterEnded != null)
				{
					OnTransitionEnterEnded();
				}
			}
			else if (endValue == 1f && OnTransitionExitEnded != null)
			{
				OnTransitionExitEnded();
			}
			if (OnTransitionEnded != null)
			{
				OnTransitionEnded();
			}
			if (endValue == 0f)
			{
				_currentMaterial = null;
			}
			_transitionCoroutine = null;
		}
	}
}
